package command.login;

import command.Command;
import empty.log.Log;
import empty.users.User;
import model.log.LogModel;
import model.login.LoginModel;
import server.ServerThread;

import java.time.LocalDateTime;
import java.util.List;

public class LoginCommand extends Command {
    @Override
    public void excute() {

    }

    @Override
    public void excute(ServerThread st) {
        try {
            List<Object[]> list = new LoginModel().selectUser((User) this.o);
            if (list.size() != 0) {
                Object[] os = list.get(0);
                user = new User((int) os[0], (String) os[1], (String) os[2], (int) os[3], (int) os[4],
                        (String) os[5], (String) os[6], (int) os[7], (int) os[8]);
                this.o = user;
                // 把此线程的登录用户保存
                st.setUser(user);
                // log
                Log log = new Log("登录", user.getId(), LocalDateTime.now().toString());
                new LogModel().insertLog(log);
                this.r = true;
            } else {
                this.r = false;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            this.e = ex;
            this.r = false;
        }
    }
}
